/******************************************************************************
*
* This file is part of FreeTrack (http://code.google.com/p/qtfreetrack).
* BY Willy Gardiol (willy@gardiol.org)
*
* FreeTrack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FreeTrack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar.  If not, see <http://www.gnu.org/licenses/>
*
******************************************************************************/

#include "workersavegpx.h"
#include "trackpoint.h"
#include "trackwaypoint.h"

WorkerSaveGpx::WorkerSaveGpx(QXmlStreamWriter * x,
                             QList<TrackPoint>* pts,
                             WaypointsHash* wpts,
                             QString t,
                             QString d) :
    WorkerProcess(2)
{
    stream = x;
    points = pts;
    waypoints = wpts;
    title = t;
    description = d;
}

WorkerSaveGpx::~WorkerSaveGpx()
{
}

bool WorkerSaveGpx::do_stuff()
{
    int max_pts = points->size();
    int max_waypoints = waypoints->size();
    int i;

    sendUpdate( tr("No points saved yet"), 0, 0, max_pts, 0 );
    sendUpdate( tr("No waypoints saved yet"), 1, 0, max_waypoints, 0 );

    stream->setAutoFormatting(true);
    stream->writeStartDocument();

    stream->writeStartElement("gpx");
    stream->writeAttribute("version", "1.0");
    stream->writeAttribute("creator", "FreeTrack");
    stream->writeAttribute("xmlns", "http://www.topografix.com/GPX/1/1");
    stream->writeTextElement("name", title);
    stream->writeTextElement("desc", description);

    stream->writeTextElement("time",QDateTime::currentDateTime().toString("yyyy-MM-ddThh:mm:ssZ"));

    stream->writeStartElement("trk");
    stream->writeTextElement("name", title);

    stream->writeStartElement("trkseg");
    int stepper = 10;
    for ( i = 0; i < max_pts; i++)
    {
        if ( i == 100 )
            stepper = 100;
        else if ( i == 1000 )
            stepper = 1000;
        else if ( i == 10000 )
            stepper = 10000;
        points->at(i).toGpx(stream);
        if ( (i % stepper) == 0 )
        {
            sendUpdate( tr("Saved point n.%1").arg(i), 0, 0, max_pts, i );
            if ( please_terminate )
                return false;
        }
   }

    sendUpdate( tr("Saved point n.%1").arg(i), 0, 0, max_pts, max_pts );
    if ( please_terminate )
        return false;

    stream->writeEndElement(); // trkseg
    stream->writeEndElement(); // trk

    i = 0;
    foreach ( int k, waypoints->keys() )
    {
        waypoints->value(k).toGpx(stream);
        i++;
        sendUpdate( tr("Saved waypoint n.%1").arg(i), 1, 0, max_waypoints, i );
        if ( please_terminate )
            return false;
    }

    sendUpdate( tr("Saved waypoint n.%1").arg(i), 1, 0, max_waypoints, max_waypoints );
    if ( please_terminate )
        return false;

    stream->writeEndElement(); // gpx
    stream->writeEndDocument();
    return !please_terminate;
}
